# pip install pyqt5 pyqtwebengine

import sys
from PyQt5.QtCore import QUrl, Qt
from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    QToolBar,
    QLineEdit,
    QPushButton,
    QAction,
    QHBoxLayout,
    QVBoxLayout,
    QWidget,
    QTabWidget,
)
from PyQt5.QtGui import QIcon
from PyQt5.QtWebEngineWidgets import QWebEngineView

class Browser(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyBrowser")
        self.setGeometry(100, 100, 1200, 800)
        
        # 创建主窗口部件
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        
        # 创建布局
        layout = QVBoxLayout(main_widget)
        
        # 创建工具栏
        toolbar = QToolBar("Navigation")
        toolbar.setStyleSheet(
            """
            background-color: #2c3e50;
            color: white;
            padding: 5px;
            """
        )
        self.addToolBar(toolbar)
        
        # 地址栏
        self.address_bar = QLineEdit()
        self.address_bar.setPlaceholderText("输入网址（例如：https://www.example.com）")
        self.address_bar.setStyleSheet(
            """
            background-color: #34495e;
            color: white;
            border-radius: 5px;
            padding: 8px;
            """
        )
        self.address_bar.returnPressed.connect(self.navigate_to_url)
        
        # 后退按钮
        back_action = QAction("返回", self)
        back_action.setIcon(QIcon.fromTheme("go-previous"))
        back_action.triggered.connect(lambda: self.current_tab().back())
        
        # 前进按钮
        forward_action = QAction("前进", self)
        forward_action.setIcon(QIcon.fromTheme("go-next"))
        forward_action.triggered.connect(lambda: self.current_tab().forward())
        
        # 刷新按钮
        refresh_action = QAction("刷新", self)
        refresh_action.setIcon(QIcon.fromTheme("view-refresh"))
        refresh_action.triggered.connect(lambda: self.current_tab().reload())
        
        # 将工具栏控件添加到布局中
        toolbar.addWidget(self.address_bar)
        toolbar.addAction(back_action)
        toolbar.addAction(forward_action)
        toolbar.addAction(refresh_action)
        
        # 创建标签页管理器
        self.tab_widget = QTabWidget()
        layout.addWidget(self.tab_widget)
        
        # 创建默认页面
        self.create_new_tab(QUrl("https://cn.bing.com"), "新标签")
    
    def current_tab(self):
        return self.tab_widget.currentWidget().view
    
    def create_new_tab(self, url=None, title="新标签"):
        # 创建一个新的标签页
        tab = QWidget()
        
        # 创建布局和控件
        tab_layout = QVBoxLayout(tab)
        view = QWebEngineView()
        view.setUrl(url if url else QUrl("https://www.google.com"))
        
        # 设置导航栏的URL与当前页面同步
        view.urlChanged.connect(lambda qurl: self.address_bar.setText(qurl.toString()))
        
        tab_layout.addWidget(view)
        self.tab_widget.addTab(tab, title)
        self.tab_widget.setCurrentWidget(tab)
        
        return view
    
    def navigate_to_url(self):
        # 获取地址栏输入的URL
        url = QUrl(self.address_bar.text())
        if url.scheme() == "":
            url.setScheme("http")
        self.current_tab().setUrl(url)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    browser = Browser()
    browser.show()
    sys.exit(app.exec_())